/*
Copyright (c) 2006 - 2008  Universitaet Osnabrueck, virtUOS
Authors: Markus Ketterl, Johannes Emden, Nils Schuhmacher, Andreas Rickling

This file is part of virtPresenter.

virtPresenter is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

virtPresenter is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with virtPresenter.  If not, see <http://www.gnu.org/licenses/>.
*/

package virtPresenter.commands
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import virtPresenter.event.*;
	import virtPresenter.model.*;
	import virtPresenter.event.NextSlideEvent;
	import com.adobe.cairngorm.control.*;
	import mx.core.Application;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	
	/**
	 * Command lets the presentation advance to the next slide.
	 * */
  	public class GoToNextSlide implements ICommand {		
		
		/**
		 * Handles a GoToNextSlideEvent and lets the presentation go to the next slide.
		 * Sets the VideoPlayer to the timestep of the next slide and therefor invokes
		 * a VideoPlayerTimeChangeEvent through the AdvancedScrubber.
		 * */	
		public function execute( event : CairngormEvent ): void {				
			   				   				
				var model : virtPresenterLocator = virtPresenterLocator.getInstance();
			    var nextSlideTimeStep:TimeStep=model.xmlParser.getNextSlideStep(model.currentvideotime*1000);
			    var nextSlide_time:int=nextSlideTimeStep.getTime();
			    var time_sec:Number=nextSlide_time/1000+0.5;
			   
			    trace("GoToNextSlide. TimeStep: " + nextSlideTimeStep.toString());
			    //trace("GoToNextSlide. Advance to time (in msec from XML): " + nextSlide_time);
			    //trace("GoToNextSlide. Advance to time (in sec): " + time_sec);
			    //trace("GoToNextSlide. Current video time: " + model.currentvideotime);			    
			    
			    // If the next timestep is identical with the current, the last slide has been reached
			    // (see getNextSlideStep() in XMLParser).
			    // If you are on the last slide and want to advance a PlayStopVideoPlayerEvent is dispatched
			    // which stops the video.
			    if(nextSlideTimeStep==model.xmlParser.getTimeStepByTime(model.currentvideotime*1000))
			    {
			    	CairngormEventDispatcher.getInstance().dispatchEvent(new PlayStopVideoPlayerEvent(PlayStopVideoPlayerEvent.STOP));
			    }
			    
			    else
			    {
				     model.netStream.seek(time_sec);
			    }
			   
			    
			   
			   			
		}
	
		
	}
	
	
}